﻿using Microsoft.Xna.Framework.Graphics;

namespace TrinityEngine
{
    public class Animation : Renderer
    {
        public Sprite[] frames;

        public float duration;

        public bool isPlaying;

        private int _index;

        private float _timeCounter;

        public Animation(Sprite[] frames, float duration)
        {
            this.frames = frames;
            this.duration = duration;
            isPlaying = false;
            _index = 0;
            _timeCounter = 0f;
        }

        public void Play()
        {
            isPlaying = true;
        }

        public void Pause()
        {
            isPlaying = false;
        }

        public void Stop()
        {
            isPlaying = false;
            _index = 0;
            _timeCounter = 0f;
        }

        protected override void Update()
        {
            base.Update();

            if (isPlaying == true)
            {
                if (_timeCounter >= duration)
                {
                    _index++;

                    if (_index >= frames.Length)
                    {
                        _index = 0;
                    }

                    _timeCounter = 0f;
                }
                else
                {
                    _timeCounter += Time.deltaTime;
                }
            }
        }

        protected override void Render()
        {
            if (frames == null || frames.Length == 0)
            {
                return;
            }

            /*_origin = new Vector2(frames[_index].texture.width * pivot.x, frames[_index].texture.height * pivot.y);*/
            /*Engine.Instance.batch.Draw(
                frames[_index].texture,
                new Vector2(_position.x, -_position.y),
                null,
                tint,
                _rotation,
                _origin,
                _scale / frames[_index].pixelPerUnit,
                SpriteEffects.None,
                sortingLayer
            );*/
        }

        protected override void OnDestroy()
        {
            isPlaying = false;
            frames = null;
        }
    }
}
